﻿using Sidvall.Serialization;
using Sidvall.UI.Entities;
using System.Collections.Generic;

namespace Sidvall.UI.Serialization
{
    public class SerializerManager
    {
        #region Public Members

        public ISerializer Serializer { get; set; }

        #region SerializeBusinessObject

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public string SerializeBusinessObject(object value, Formatting formatting, bool ignoreBusinessBaseState, bool ignoreDataItem)
        {
            return SerializeBusinessObject(value, formatting, ignoreBusinessBaseState, ignoreDataItem, null, DisplayPositionPolicy.None, null);
        }
        public string SerializeBusinessObject(object value, Formatting formatting, bool ignoreBusinessBaseState, bool ignoreDataItem, IEnumerable<PropertyItem> propertyItems,
            DisplayPositionPolicy defaultDisplayPositionPolicy, int? defaultDisplayPosition)
        {
            var settings = new SerializerSettings()
            {
                Formatting = formatting,
                DefaultDisplayPositionPolicy = defaultDisplayPositionPolicy,
                DefaultDisplayPosition = defaultDisplayPosition,
            };
            if (ignoreBusinessBaseState)
            {
                settings.PropertyItems.AddExcludedItem(nameof(BusinessBase.DataRowBusinessRules));
                settings.PropertyItems.AddExcludedItem(nameof(BusinessBase.DataRowStateIsDeleted));
                settings.PropertyItems.AddExcludedItem(nameof(BusinessBase.DataRowStateIsDirty));
                settings.PropertyItems.AddExcludedItem(nameof(BusinessBase.DataRowStateIsNew));
                settings.PropertyItems.AddExcludedItem(nameof(BusinessBase.DataRowStateIsSelfDirty));
            }
            if (ignoreDataItem)
            {
                settings.PropertyItems.AddExcludedItem(nameof(TreeItem.DataItem));
            }
            settings.PropertyItems.AddRange(propertyItems);
            return this.Serializer.Serialize(value, settings);
        }

        #endregion

        #endregion
        #region Factory Members

        #region Current

        private static readonly System.Lazy<SerializerManager> _Current = new System.Lazy<SerializerManager>(GetSerializerManager);
        public static SerializerManager Current
        {
            get
            {
                return _Current.Value;
            }
        }

        #endregion

        #endregion
        #region Private Members

        #region GetSerializerManager

        private static SerializerManager GetSerializerManager()
        {
            return new SerializerManager(new Sidvall.Serialization.Serializer());
        }

        #endregion

        #endregion
        #region Constructors

        public SerializerManager(ISerializer serializer)
        {
            this.Serializer = serializer;
        }

        #endregion
    }
}
